<?php
/**
 * 对比文件不同，查找不同小区
 */

fileDiff('./log/esxiaoqu.log', './log/esremote.log');

function getXiaoQuName($file) {
    $count = 0;
    $fp = fopen($file, 'r');
    
    
    $old_active = $active = $current = 'start';
    while($line = fgets($fp)) {
        $line = str_replace(array('北京市','·',' ', '-'), '', trim($line));
        
        @list($name, $addr, $baidu_geo) = explode("\t", $line);
        $reKey = array(
            'a','b','c'
            ,'A','B','C'
            ,'(','（','-',1,2,3,4,5,6,7,8,9,0 
            ,'楼','小区', '区', '宿舍', '公寓'
            ,'一', '二', '三', '四','东','西','南','北'
        );
        
        foreach ($reKey as $t) {
            if($temp = strstr($name, strval($t),true)) {
                $name = $temp;
            }
        }
        $current = $name;
        
        if (strstr($current, $active) !== false) {
            $active;
        } elseif (strstr($active, $current) !== false) {
            $active = $current;
        } else {
            $active = $current;
        }
        if ($old_active != $active) {
            $old_active = $active;
            $key = mb_substr($active, 0, 1, "UTF-8");
            //$py = Pinyin::trans(mb_substr($active, 0, 1, "UTF-8"));
            //$key = substr($py, 0, 2);
            if (!isset($return[$key][$active])) {
                $count++;
                $return[$key][$active] = $addr . "\t" . $baidu_geo;
            }
        }
    }
    
    $file2 = $file.'.res.log';
    $fp2 = fopen($file2, 'w+');
    foreach ($return as $py => $data) {
        foreach ($data as $xiaoqu => $v) {
            fwrite($fp2, $xiaoqu . ":" . $v . "\n");
        }
    }
    
    fclose($fp2);
    fclose($fp);
    return array($return, $count);
}

function fileDiff($file, $file2) {
    list($list_1, $count_1) = getXiaoQuName($file);
    list($list_2, $count_2) = getXiaoQuName($file2);
    $count = 0;
    $count_baidu = 0;
    foreach ($list_1 as $py => & $data_1) {
        foreach ($data_1 as $xiaoqu => & $v_1) {
            if (isset($list_2[$py][$xiaoqu])) {
                unset($list_2[$py][$xiaoqu]);
                $count++;
                $v_1 = 0 . ":\t" . $v_1;
            } else {
                $count_baidu++;
                $v_1 = 1 . ":\t" . $v_1;
            }
        }
    }
    
    $count_esf = 0;
    foreach ($list_2 as $py => $data_2) {
        foreach ($data_2 as $xiaoqu => $v_2) {
            if (isset($list_1[$py][$xiaoqu])) {
                $count++;
                $list_1[$py][$xiaoqu] = 0 . ":\t" . $v_2;
            } else {
                $count_esf++;
                $list_1[$py][$xiaoqu] = 2 . ":\t" . $v_2;
            }
        }
    }
    
    echo "文件：",$file,":百度共有小区：",$count_1,"\n";
    echo "文件：",$file2,":二手房共有小区：",$count_2,"\n";
    
    echo "0 : 数据：都有",':',$count,"\n";
    echo "1 : 数据：未覆盖（百度有我们没有）",':',$count_baidu,"\n";
    echo "2 : 数据：我们有百度没有",':',$count_esf,"\n";
    
    foreach ($list_1 as $py => $data_1) {
        foreach ($data_1 as $xiaoqu => $v_1) {
            $return[] = $v_1 . "\t" .  $xiaoqu;
        }
    }
    sort($return);
    foreach ($return as $echo) {
        echo $echo . "\n";
    }
    return $return;
}



